package com.qch.edu.lagou.strmatch;

/**
 * @Author qch
 * @Date 2021/7/21
 * hash匹配O（n+m）
 */
public class RKMatch {
    public static boolean isMatch(String main,String pattern)
    {
        int ph=strToHash(pattern);
        for (int i = 0; i <=(main.length() - pattern.length()); i++) {
            if(ph==strToHash(main.substring(i,i+pattern.length())))
            {
                return true;
            }
        }
        return false;
    }
    public static int strToHash(String src)
    {
        int hash=0;
        for (int i = 0; i < src.length(); i++) {
            //26进制 小写自读a-z
            //(97-97)*26^2+(98-97)*26^1+(99-97)*26^0
           // hash*=26;
            //hash+=src.charAt(i)-97;
            hash+=(src.charAt(i)-97)*Math.pow(26,i);
        }
        return hash;
    }

    public static void main(String[] args) {
        System.out.println(strToHash("cbc"));
        System.out.println(isMatch("hello","loa"));
    }
}
